//
//  YHHomeViewModel.swift
//  WeiBoYH
//
//  Created by yanhui on 2017/1/9.
//  Copyright © 2017年 yanhui. All rights reserved.
//

import UIKit

class YHHomeViewModel: NSObject {
    
      lazy var viewmodelArray: [YHHomeCellModel] = [YHHomeCellModel]()
    func loadData(finished: @escaping (Bool) -> ()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        guard let access_token = YHUserAccountViewModel.shard.access_token else {
            return
        }
        let parameters = ["access_token" : access_token ]
        print(parameters)
        HMNetworkTools.shared.request(method: .GET, urlString: urlString, parameters: parameters) { (resp, error) in
            if error != nil {
                finished(false)
                return
            }
            let dict = resp as! [String : Any]
            //通过statuses来获取微博数组
            let array = dict["statuses"] as! [[String : Any]]
            //遍历数字 字典转模型对象
            var tempArray = [YHHomeCellModel]()
            for item in array {
                //实例化视图模型
                let cellModle = YHHomeCellModel()
                let s = YHStatus()
                //字典转换模型
                s.yy_modelSet(with: item)
                cellModle.status = s
                //添加到数组中
                tempArray.append(cellModle)
            }
            //保存到临时数组
            self.viewmodelArray = tempArray
            //执行成功的回调
            finished(true)
        }
    }

    
}
